const shopping = require("express").Router()
const sqlOptions = require("../../sql");

// 加入购物车
shopping.post("/save", async (req, res) => {
    try {
        const {product_id, colorId, num} = req.body
        const user_id = req.rows.id
        const product = await req.db.query(sqlOptions({
            name: "sms_product",
            method: "list",
            data: {
                product_id
            }
        }))
        console.log(product)
        if (product.length) {
            const obj = product[0]
            const shoppings = await req.db.query(sqlOptions({
                name: "sms_shopping_cart",
                method: "list",
                data: {
                    product_id,
                    user_id
                },
                if: "AND"
            }))
            if (shoppings.length) {
                return res.status(200).send({
                    code: 400,
                    message: "已经加入购物车, 无需继续添加",
                    data: ''
                })
            }
            const data = {
                product_id,
                cart_num: num,
                color_id: colorId || null,
                cart_lose: '1',
                user_id: user_id,
                product_name: obj.product_name,
                brand_id: obj.brand_id,
                category_id: obj.category_id,
                product_pic: obj.product_pic,
                cart_money: obj.product_money,
                discount: obj.discount
            }
            await req.db.query(sqlOptions({
                name: "sms_shopping_cart",
                method: "save",
                data: {
                    product_id,
                    cart_num: num,
                    color_id: colorId || "",
                    cart_lose: '1',
                    user_id: user_id,
                    product_name: obj.product_name,
                    brand_id: obj.brand_id,
                    category_id: obj.category_id,
                    product_pic: obj.product_pic,
                    cart_money: obj.product_money,
                    discount: obj.discount
                }
            }))
            return res.status(200).send({
                code: 200,
                message: "加入购物车成功",
                data: ''
            })
        }
        return res.status(200).send({
            code: 400,
            message: "该商品已下架无法加入购物车",
            data: ''
        })

    } catch (e) {
        return res.status(500).send({
            code: 500,
            message: "系统错误",
            data: ""
        })
    }
})
// 移出购物车
shopping.delete("/delete", async (req, res) => {
    try {
        const {id} = req.query
        await req.db.query(sqlOptions({
            method: "delete",
            name: "sms_shopping_cart",
            data: {
                id
            }
        }))
        return res.status(200).send({
            code: 200,
            message: "删除成功",
            data: ''
        })

    } catch (e) {
        return res.status(500).send({
            code: 500,
            message: "系统错误",
            data: ""
        })
    }
})
// 查询购物车所有
shopping.post("/list", async (req, res) => {
    try {
        const user_id = req.rows.id
        const list = await req.db.query(sqlOptions({
            method: "list",
            name: "sms_shopping_cart",
            data: {
                user_id
            },
        }))

        return res.status(200).send({
            code: 200,
            message: "",
            data: list
        })
    } catch (e) {
        return res.status(500).send({
            code: 500,
            message: "系统错误",
            data: ""
        })
    }
})
// 结算导入到确认订单表中
shopping.post("/order", async (req, res) => {
    try {
        const user_id = req.rows.id
        const list = req.body.map((item) => {
            const obj = item
            obj.shopping_id = item.id
            obj.user_id = user_id
            delete obj.id
            return obj
        })

        console.log(list)
        const users = await req.db.query(sqlOptions({
            name: "oms_shopping_order",
            method: "list",
            data: {
                user_id
            }
        }))

        if (users.length) {
            await req.db.query(sqlOptions({
                name: "oms_shopping_order",
                method: "delete",
                data: {
                    user_id
                }
            }))
        }
        await req.db.query(sqlOptions({
            name: "oms_shopping_order",
            method: "save",
            data: list
        }))
        return res.status(200).send({
            code: 200,
            message: "添加成功",
            data: ""
        })
    } catch (e) {
        console.log(e)
        return res.status(200).send({
            code: 500,
            message: "系统错误",
            data: ""
        })
    }
})

module.exports = shopping
